package com.leyou.item.service.impl;

import com.leyou.item.interfaces.pojo.SpecGroup;
import com.leyou.item.interfaces.pojo.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 对规格参数的相关业务逻辑
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {

    /**
     * 注入ParamMapper
     */
    @Autowired
    private SpecParamMapper paramMapper;

    @Autowired
    private SpecGroupMapper groupMapper;

    /**
     * 根据条查询规格参数
     * @param cid
     * @param gid
     * @param generic
     * @param searching
     * @return
     */
    @Override
    public List<SpecParam> queryParam(Long cid, Long gid, Boolean generic, Boolean searching) {
        // 设置查询条件
        Example example = new Example(SpecParam.class);
        Example.Criteria criteria = example.createCriteria();
//        example.excludeProperties("numeric");
        if(cid != null) {
            criteria.andEqualTo("cid",cid);
        }
        if(gid != null) {
            criteria.andEqualTo("groupId",gid);
        }
        if (generic != null) {
            criteria.andEqualTo("generic",generic);
        }
        if(searching != null) {
            criteria.andEqualTo("searching",searching);
        }
        List<SpecParam> paramData = paramMapper.selectByExample(example);
        return paramData;
    }

    @Override
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        List<SpecGroup> select = groupMapper.select(record);
        return select;
    }

    @Override
    public List<SpecGroup> queryGroupsWithParam(Long cid) {
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(group -> {
            List<SpecParam> Params = this.queryParam(null, group.getId().longValue(), null, null);
            group.setParams(Params);
        });
        return groups;
    }
}
